package com.jade.linkong.controller;

import java.io.IOException;
import java.util.List;

import com.jade.common.core.text.Convert;
import com.jade.common.utils.poi.ExcelUtil;
import com.jade.linkong.domain.LkNotice;
import com.jade.linkong.service.ILkNoticeService;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jade.common.annotation.Log;
import com.jade.common.enums.BusinessType;
import com.jade.common.core.controller.BaseController;
import com.jade.common.core.domain.AjaxResult;
import com.jade.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 告示Controller
 * 
 * @author jade
 * @date 2020-07-16
 */
@Controller
@RequestMapping("/linkong/notice")
public class LkNoticeController extends BaseController
{
    private String prefix = "linkong/notice";

    @Autowired
    private ILkNoticeService lkNoticeService;

    @RequiresPermissions("linkong:notice:view")
    @GetMapping()
    public String notice()
    {
        return prefix + "/notice";
    }

    /**
     * 查询告示列表
     */
    @RequiresPermissions("linkong:notice:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LkNotice lkNotice)
    {
        startPage();
        List<LkNotice> list = lkNoticeService.selectLkNoticeList(lkNotice);
        return getDataTable(list);
    }

    /**
     * 导出告示列表
     */
    @RequiresPermissions("linkong:notice:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LkNotice lkNotice)
    {
        List<LkNotice> list = lkNoticeService.selectLkNoticeList(lkNotice);
        ExcelUtil<LkNotice> util = new ExcelUtil<LkNotice>(LkNotice.class);
        return util.exportExcel(list, "notice");
    }

    /**
     * 新增告示
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存告示
     */
    @RequiresPermissions("linkong:notice:add")
    @Log(title = "告示", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LkNotice lkNotice)
    {
        return toAjax(lkNoticeService.insertLkNotice(lkNotice));
    }

    /**
     * 修改告示
     */
    @GetMapping("/edit/{lkNoticeId}")
    public String edit(@PathVariable("lkNoticeId") Long lkNoticeId, ModelMap mmap)
    {
        LkNotice lkNotice = lkNoticeService.selectLkNoticeById(lkNoticeId);
        mmap.put("lkNotice", lkNotice);
        return prefix + "/edit";
    }

    /**
     * 修改保存告示
     */
    @RequiresPermissions("linkong:notice:edit")
    @Log(title = "告示", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LkNotice lkNotice)
    {
        return toAjax(lkNoticeService.updateLkNotice(lkNotice));
    }

    /**
     * 删除告示
     */
    @RequiresPermissions("linkong:notice:remove")
    @Log(title = "告示", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(lkNoticeService.deleteLkNoticeByIds(ids));
    }

    /**
     * 预览告示单
     * @param id
     * @return
     */
//    @RequiresPermissions("linkong:notice:preview")
//    @GetMapping("/preview/{id}")
//    @ResponseBody
//    public AjaxResult preview(@PathVariable("id")Long id){
//        String data = lkNoticeService.previewNotice(id);
//        return AjaxResult.success("成功",data);
//    }

    @GetMapping("/preview2/{id}")
    public String preview2(@PathVariable("id")Long id, ModelMap modelMap){
        String data = lkNoticeService.previewNotice(id);
        modelMap.put("data", data);
        return prefix + "/preview";
    }

    @GetMapping("/preview")
    public String preview3(LkNotice notice, ModelMap modelMap){
        String data = lkNoticeService.previewNotice(notice);
        modelMap.put("data", data);
        return prefix + "/preview";
    }

    /**
     * 导出告示单
     * @param response
     * @param id
     * @throws IOException
     */
    @RequiresPermissions("linkong:notice:exportNotice")
    @Log(title = "导出告示单", businessType = BusinessType.EXPORT)
    @GetMapping("/exportNotice/{id}")
    public void exportNotice(HttpServletResponse response, @PathVariable("id") Long id) throws IOException {
        byte[] data = lkNoticeService.exportNotice(id);
        genCode(response, data);
    }
    @RequiresPermissions("linkong:notice:exportNotice")
    @Log(title = "导出告示单", businessType = BusinessType.EXPORT)
    @GetMapping("/batchEExportNotice")
    @ResponseBody
    public void batchExportNotice(HttpServletResponse response, String tables) throws IOException{
        String[] tableIds = Convert.toStrArray(tables);
        byte[] data = lkNoticeService.exportNotice(tableIds);
        genCode(response, data);
    }

    /**
     * 签字已阅
     * @param id
     * @return
     */
    @RequiresPermissions("linkong:notice:signature")
    @Log(title = "告示", businessType = BusinessType.UPDATE)
    @PostMapping("/signature")
    @ResponseBody
    public AjaxResult signature(Long id){
        LkNotice notice = lkNoticeService.selectLkNoticeById(id);
        notice.setIsRead("1");
        return toAjax(lkNoticeService.updateLkNotice(notice));
    }

    /**
     * 生成zip文件
     */
    private void genCode(HttpServletResponse response, byte[] data) throws IOException
    {
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"doc.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }

}
